<?php
class Html extends CHtml {
	/**
	 * Creates absolute link
	 */
	public static function absoluteLink($text, $url = '#', $htmlOptions = array(), $rememberClient = false) {
		$url = Yii::app ()->request->getHostInfo () . parent::normalizeUrl ( $url );
		return parent::link ( $text, $url, $htmlOptions, $rememberClient );
	}
	/**
	 * Makes the given URL relative to the /image directory
	 */
	public static function imageUrl($url) {
		return Yii::app ()->theme->baseUrl . '/images/' . $url;
	}
	/**
	 * Makes the given URL relative to the /css directory
	 */
	public static function cssUrl($url) {
		return Yii::app ()->theme->baseUrl . '/css/' . $url;
	}
	/**
	 * Makes the given URL relative to the /js directory
	 */
	public static function jsUrl($url) {
		return Yii::app ()->theme->baseUrl . '/js/' . $url;
	}
	public static function jqueryCssUrl($url) {
		return Yii::app ()->theme->baseUrl . '/js/jqueryUI/theme/' . $url;
	}
	public static function addClass(&$htmlOptions, $class) {
		if (isset ( $htmlOptions ['class'] ))
			$htmlOptions ['class'] .= ' ' . $class;
		else
			$htmlOptions ['class'] = $class;
	}
	
	public static function submitButton($label = 'submit', $htmlOptions = array()) {
		self::addClass ( $htmlOptions, 'submit' );
		return parent::submitButton ( $label, $htmlOptions );
	}
	public static function activeCheckBox($model, $attribute, $htmlOptions = array()) {
		self::addClass ( $htmlOptions, 'checkbox' );
		return parent::activeCheckBox ( $model, $attribute, $htmlOptions );
	}
	public static function checkBox($name, $checked = false, $htmlOptions = array()) {
		self::addClass ( $htmlOptions, 'checkbox' );
		return parent::checkBox ( $name, $checked, $htmlOptions );
	}
	/**
	 * Creates a date/time field (which is actually contains 4 sub-fields).
	 * The model must contain the attribute argumented to this method, and in addition
	 * must have the attributes: $attribute.'H', $attribute.'M', $attribute.'PM'.  Works
	 * well with the TimeBehavior.  Unfortunitally the behavior can not create the extra needed
	 * attributes for you, so you need to add them to the model yourself
	 * 
	 * @param mixed $model
	 * @param mixed $attribute
	 * @param mixed $htmlOptions
	 */
	public static function activeDatetime($model, $attribute, $htmlOptions = array()) {
		self::resolveNameID ( $model, $attributeD = $attribute . 'D', $htmlOptions );
		$nameId = $htmlOptions ['id'];
		$script = <<<EOD
                var activity;
                $("#$nameId").datepicker();
EOD;
		Yii::app ()->clientScript->registerScript ( 'datetime_' . $nameId, $script, CClientScript::POS_READY );
		JavaScript::calenderLoad ();
		$hours = array (0 => 'Hour' );
		for($i = 1; $i <= 12; $i ++)
			$hours [$i] = $i;
		$minutes = array (- 1 => 'Minute' );
		for($i = 0; $i <= 59; $i ++)
			$minutes [$i] = str_pad ( $i, 2, '0', STR_PAD_LEFT );
		return self::activeTextField ( $model, $attributeD, array ('style' => 'width:100px' ) ) . ' ' . self::activeDropDownList ( $model, $attribute . 'H', $hours ) . ':' . self::activeDropDownList ( $model, $attribute . 'M', $minutes ) . ' ' . self::activeDropDownList ( $model, $attribute . 'PM', array (0 => 'am', 1 => 'pm' ) );
	}
        
        public static function formatDate($date){
            if ($date == null)
                return null;
            $timestamp = CDateTimeParser::parse($date,'yyyy-MM-dd hh:mm:ss');
            return CTimestamp::formatDate('M d, Y',$timestamp);
        }
        
        /**
         *return time format follow : Fri 5,2010
         * @param type $time : string query from database;
         */
        
        public static function formatTime($time){
        	if ($time == null)
                return null;
            $timestamp = CDateTimeParser::parse($time,'yyyy-MM-dd hh:mm:ss');
        	return CTimestamp::formatDate('h:i a',$timestamp);
        }
        
        public static function formatDateTime($data,$inputPattern,$outputPattern){
           if ($data == null)
                return null;
            $timestamp = CDateTimeParser::parse($data,$inputPattern);
            return CTimestamp::formatDate($outputPattern,$timestamp);
        }
        
        
        /**
         * Lay thoi gian so voi thuc tai
         */
        public static function spaceTime($datetime){
                if ($datetime == null)
                    return "just now";
                $now = strtotime('now');
                $timestamp = CDateTimeParser::parse($datetime,'yyyy-MM-dd hh:mm:ss');
                $space = ($now - $timestamp);
                Yii::log("space:".$space."|timestamp:".$timestamp."|now:".$now);
                if ($space <=0 )
                    return "just now";
                if ($space <60 )
                    return "about ".number_format($space,0)." seconds ago";
                
                $space = $space / 60;
                if ($space <60 )
                    return "about ".number_format($space,0)." minutes ago";
                
                $space = $space / 60;
                if ($space <24 )
                    return "about ".number_format($space,0)." hours ago";
                
                $space = $space / 24;
                if ($space <7 )
                    return "about ".number_format($space,0)." days ago";
                
                $space = $space / 7;
                if ($space <4 )
                    return "about ".number_format($space,0)." weeks ago";
                
                $space = $space / 4;
                return "about ".number_format($space,0)." months ago";
        }
        
        /*
         * space date
         */
        public static function spaceDate($date) {
        	$timestamp = CDateTimeParser::parse($date,'yyyy-MM-dd hh:mm:ss');
        	$today = date('Y-m-d');
        	$now = strtotime('now');
        	$tomorrow = strtotime('+1 day');
        	$tomorrow = date('Y-m-d', $tomorrow);
        	
        	$date = explode(" ",$date);
        	$date = $date[0];
        	
        	Yii::log("1.".$today);
        	Yii::log("2.".$date);
        	
        	$space = $timestamp  - $now;
        	
        	if ($space < 0)
        		return 'ELAPSED';
        	if ($date == $today)
        		return 'TODAY';
        	if($date == $tomorrow)
        		return 'TOMORROW';
        	return 'LATER';
        }
       public static function duration($start, $finish)
       {
       		$timestampStart = CDateTimeParser::parse($start,'yyyy-MM-dd hh:mm:ss');
       		$timestampFinish = CDateTimeParser::parse($finish,'yyyy-MM-dd hh:mm:ss');
       		$duration = round(($timestampFinish - $timestampStart) / (3600));
       		return $duration;
       }
       
       //month of year
       public static function getMonth()
       {
       		return array(
       			'1'=>'Tháng 1',
       			'2'=>'Tháng 2',
				'3'=>'Tháng 3',
				'4'=>'Tháng 4',
				'5'=>'Tháng 5',
				'6'=>'Tháng 6',
				'7'=>'Tháng 7',
				'8'=>'Tháng 8',
				'9'=>'Tháng 9',
				'10'=>'Tháng 10',
				'11'=>'Tháng 11',
				'12'=>'Tháng 12'
				
			);
       }
       
       //year
       public static function getYear()
       {
       		return array(
       			'1980'=>'Năm 1980',
       			'1981'=>'Năm 1981',
				'1982'=>'Năm 1982',
				'1983'=>'Năm 1983',
				'1984'=>'Năm 1984',
				'1985'=>'Năm 1985',
				'1986'=>'Năm 1986',
				'1987'=>'Năm 1987',
				'1988'=>'Năm 1988',
				'1989'=>'Năm 1989',
				'1990'=>'Năm 1990',
				'1991'=>'Năm 1991',
				'1992'=>'Năm 1992',
				'1993'=>'Năm 1993',
				'1994'=>'Năm 1994',
				'1995'=>'Năm 1995',
				'1996'=>'Năm 1996',
				'1997'=>'Năm 1997',
				'1998'=>'Năm 1998',
				'1999'=>'Năm 1999',
				'2000'=>'Năm 2000',
				'2001'=>'Năm 2001',
				'2002'=>'Năm 2002',
				'2003'=>'Năm 2003',
				'2004'=>'Năm 2004',
				'2005'=>'Năm 2005',
				'2006'=>'Năm 2006',
				'2007'=>'Năm 2007',
				'2008'=>'Năm 2008',
				'2009'=>'Năm 2009',
				'2010'=>'Năm 2010',
				'2011'=>'Năm 2011',
				'2012'=>'Năm 2012'
				
				
       		);
       }
}